package top.tupobi.zing.mvp.view;

import android.os.Build;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.google.android.material.textfield.TextInputEditText;

import java.util.List;
import java.util.Objects;

import top.tupobi.zing.InfoRecord;
import top.tupobi.zing.R;
import top.tupobi.zing.base.BaseMVPActivity;
import top.tupobi.zing.mvp.presenter.LoginPresenter;
import top.tupobi.zing.utils.PermissionRequestCallback;
import top.tupobi.zing.utils.PermissionUtils;

public class AtyLogin extends BaseMVPActivity<LoginPresenter> implements ILoginView{

    private TextInputEditText tiet_username, tiet_password;

    @Override
    protected void setLayout() {
        if (InfoRecord.isLogin()) {
            AtyChat.actionStart(this);
            finish();
        }
        super.setLayout();
    }

    @Override
    protected int inflateContentView() {
        return R.layout.aty_login;
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void process() {

        PermissionUtils.requestAllPermissionWithRx(this, new PermissionRequestCallback() {
            @Override
            public void onPermissionGranted(List<String> data) {
                LogUtils.e("granted permission == " + data);
            }

            @Override
            public void onPermissionRejected(List<String> data) {
                LogUtils.e("rejected permission == " + data);
            }

            @Override
            public void onPermissionRationale() {

            }
        });
    }

    @Override
    protected void initView() {
        tiet_username = findViewById(R.id.tiet_username);
        tiet_password = findViewById(R.id.tiet_password);
        ImageView iv_login_bg = findViewById(R.id.iv_login_bg);
        Glide.with(AtyLogin.this).load(R.drawable.bg_login).into(iv_login_bg);
    }

    @Override
    protected void setViewListener() {
        findViewById(R.id.btn_normal_login).setOnClickListener(this);
        findViewById(R.id.btn_register).setOnClickListener(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_normal_login:
                mPresenter.login(Objects.requireNonNull(tiet_username.getText()).toString(), Objects.requireNonNull(tiet_password.getText()).toString(), this);
                break;
            case R.id.btn_register:
                mPresenter.register(Objects.requireNonNull(tiet_username.getText()).toString(), Objects.requireNonNull(tiet_password.getText()).toString());
                break;
        }
    }
}
